require 'fileutils'
require 'qiniu'
require 'digest'
require 'openssl'
require 'base64'
require 'time'
require 'net/http'
require 'uri'
class SignApi < Grape::API
	WillPaginate.per_page = 10
	version 'v1', using: :path
	format :json
	default_format :json
	formatter :json, Grape::Formatter::Rabl
	resource :sign do
		#### 补卡处理 ####
		desc '补卡处理'
		params do
			optional :user_id, type: String, desc: '用户名'
			optional :supplementary_id, type: String, desc: '补卡记录ID'
			optional :supplementary_reason, type: String, desc: '补卡记录ID'
			optional :supplementary_status, type: String, desc: '补卡记录处理，1、申请补卡。2通过补卡，3不通过补卡'
		end
		#####===需要测试的地方########
		post 'deal_supplementary' do
			if  params[:user_id].nil? || params[:supplementary_id].nil? || params[:supplementary_status].nil?
				{code: "400", msg: "参数错误"}
			else
				sign_supplementary = SignSupplementary.find(params[:supplementary_id])
				if sign_supplementary.nil?
					{
						code: "400", 
						msg: "未查询到补卡申请"
					}
				else
					if params[:supplementary_status] == "1"
						supplementary_reason = params[:supplementary_reason].nil? ? "" : params[:supplementary_reason]
						sign_supplementary.supplementary_content = sign_supplementary.supplementary_content + "申请原因：" + supplementary_reason
						sign_supplementary.reason = params[:supplementary_reason]
						sign_supplementary.supplementary_status = params[:supplementary_status]
						sign_supplementary.save
					elsif params[:supplementary_status]== "2"
						project_user = ProjectUser.find(sign_supplementary.project_user_id)
						if sign_supplementary.suppl_type == 1
							sign = Sign.where(user_id: project_user.user_id, project_id: project_user.project_id, year: sign_supplementary.supplementary_date.year, month: sign_supplementary.supplementary_date.month, day: sign_supplementary.supplementary_date.day).first
							sign.update_at = sign_supplementary.supplementary_date.year.to_s + "-" + sign_supplementary.supplementary_date.month.to_s + "-" + sign_supplementary.supplementary_date.day.to_s + " " + "08:30:00 +0800"
							sign.save
							sign_supplementary.supplementary_status = 2
							sign_supplementary.save	
						elsif sign_supplementary.suppl_type == 2
							sign = Sign.where(user_id: project_user.user_id, project_id: project_user.project_id, year: sign_supplementary.supplementary_date.year, month: sign_supplementary.supplementary_date.month, day: sign_supplementary.supplementary_date.day).first
							sign.create_at = sign_supplementary.supplementary_date.year.to_s + "-" + sign_supplementary.supplementary_date.month.to_s + "-" + sign_supplementary.supplementary_date.day.to_s + " " + "17:30:00 +0800"
							sign.save
							sign_supplementary.supplementary_status = 2
							sign_supplementary.save	
						else
							sign = Sign.new
							sign.user_id = project_user.user_id
							sign.project_id = sign_supplementary.project_id
							sign.year = sign_supplementary.supplementary_date.year
							sign.month = sign_supplementary.supplementary_date.month
							sign.day = sign_supplementary.supplementary_date.day
							sign.create_at = sign_supplementary.supplementary_date.year.to_s + "-" + sign_supplementary.supplementary_date.month.to_s + "-" + sign_supplementary.supplementary_date.day.to_s + " " + "08:00:00 +0800"
							sign.update_at = sign_supplementary.supplementary_date.year.to_s + "-" + sign_supplementary.supplementary_date.month.to_s + "-" + sign_supplementary.supplementary_date.day.to_s + " " + "17:30:00 +0800"
							sign.save
							sign_supplementary.supplementary_status = 2
							sign_supplementary.save	
						end	
					elsif params[:supplementary_status]== "3"
						sign_supplementary.supplementary_status = params[:supplementary_status]
						sign_supplementary.save
					end
					{
						code: "0", 
						msg: "处理成功"
					}
				end
				
			end
		end


		#### 查询缺卡记录  ####
		desc '查询缺卡记录'
		params do
			optional :user_id, type: String, desc: '用户名'
			optional :project_id, type: String, desc: '项目ID'
			optional :page, type: String, desc: '页码'
		end
		get 'sign_supplementaries' do
			if params[:user_id].nil? || params[:project_id].nil? 
				{code: "400", msg: "参数错误"}
			else
				if params[:page].nil?
					page = 1
				else
					page = params[:page]
				end
				project_user = ProjectUser.where(user_id: params[:user_id], project_id: params[:project_id]).first
				if project_user.nil?
					{code: "400", msg: "参数错误"}
				else
					if project_user.role == 1 || project_user.role == 2
						sign_supplementaries = SignSupplementary.where(project_id: params[:project_id], supplementary_status: 1)
					else
						sign_supplementaries = SignSupplementary.where(project_id: params[:project_id], project_user_id: project_user.id)
					end
					sign_supplementary_list = []
					sign_supplementaries.each do |ss|
						supplementary = Hash.new
						supplementary["id"] = ss.id
						supplementary["supplementary_content"] = ss.supplementary_content
						supplementary["supplementary_status"] = ss.supplementary_status
						supplementary["created_at"] = ss.created_at.nil? ? "" : ss.created_at.strftime("%Y-%m-%d")
						sign_supplementary_list << supplementary
					end
					count = sign_supplementaries.size
					next_page = params[:page].to_i + 1
					{
						code: "0", 
						msg: "OK",
						sign_supplementary_list: sign_supplementary_list,
						count: count,
						next_page: next_page
					}
				end
				
			end
		end
	end
end